﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using MyWeb.Data;

namespace MyWeb.Datamining.SearchTextMining
{
    internal class clsTextMining
    {
        private readonly SqlDataProvider _sqlDataProvider = new SqlDataProvider();

        public int[] TextMining(string textToSearch, string resourceText)
        {
            var text = new StringBuilder(textToSearch);
            var count = new int[2];
            for (int i = 0; i < resourceText.Length; i++)
            {
                for (int j = 0; j < text.Length; j++)
                {
                    if (text[j] == resourceText[i])
                    {
                        count[0]++;
                        count[1] += Math.Abs(j - i);
                        text.Remove(j, 1);
                        break;
                    }
                }
            }

            return count;
        }

        public List<object[]> MiningResult(string textToSearch)
        {
            DataTable imagelist = _sqlDataProvider.GetTable("SELECT * FROM ImageInfo");
            var mining_resource = new List<object[]>();

            for (int i = 0; i < imagelist.Rows.Count; i++)
            {
                var imgWithOffset = new object[6];
                imgWithOffset[0] = imagelist.Rows[i][1];
                imgWithOffset[1] = imagelist.Rows[i][2];
                imgWithOffset[2] = TextMining(textToSearch, imagelist.Rows[i][2].ToString())[0];
                imgWithOffset[3] = TextMining(textToSearch, imagelist.Rows[i][2].ToString())[1];
                imgWithOffset[4] = imagelist.Rows[i][3];
                imgWithOffset[5] = imagelist.Rows[i][4];

                mining_resource.Add(imgWithOffset);
            }

            for (int i = 0; i < mining_resource.Count - 1; i++)
            {
                for (int j = i + 1; j < mining_resource.Count; j++)
                {
                    if ((int) mining_resource[i][2] < (int) mining_resource[j][2])
                    {
                        var temp = new object[5];
                        temp = mining_resource[i];
                        mining_resource[i] = mining_resource[j];
                        mining_resource[j] = temp;
                    }
                }
            }

            for (int i = 0; i < mining_resource.Count - 1; i++)
            {
                for (int j = i + 1; j < mining_resource.Count; j++)
                {
                    if ((int) mining_resource[i][2] == (int) mining_resource[j][2] &&
                        (int) mining_resource[i][3] > (int) mining_resource[j][3])
                    {
                        var temp = new object[6];
                        temp = mining_resource[i];
                        mining_resource[i] = mining_resource[j];
                        mining_resource[j] = temp;
                    }
                }
            }

            return mining_resource;
        }
    }
}